package jbase.project.system.net.http;

import jbase.project.system.exception.SysException;

public class HttpURL {

	public HttpURL(String url) {
		if (url == null) {
			throw new RuntimeException(new SysException("URL错误"));
		} else if (url.equals("")) {
			throw new RuntimeException(new SysException("URL长度过短"));
		}
		if (!url.matches("\\s*[Hh][Tt]{2}[Pp][Ss]?://[^/]+.*")) {
			throw new RuntimeException(new SysException("非法URL格式"));
		}
		this.url = url;
		this.url = this.url.replace(" ", "");
		this.url = this.url.replace("\r", "");
		this.url = this.url.replace("\n", "");
		if (!this.url.toUpperCase().startsWith("HTTP://") && !this.url.toUpperCase().startsWith("HTTPS://")) {
			throw new RuntimeException(new SysException("非法HTTP URL"));
		}
		if (this.url.toUpperCase().startsWith("HTTP://")) {
			this.protocol = this.url.substring(0, 7);
		} else if (this.url.toUpperCase().startsWith("HTTPS://")) {
			this.protocol = this.url.substring(0, 8);
		}
		String var = this.url.substring(this.protocol.length());
		if (var.indexOf("/") >= 0) {
			this.host = var.substring(0, var.indexOf("/"));
		} else {
			var += "/";
			this.host = var.substring(0, var.indexOf("/"));
		}
		this.uriSearch = var.substring(this.host.length());
		;
		String[] vars = this.uriSearch.split("\\?");
		this.uri = vars[0];
		if (vars.length > 1) {
			this.search = vars[1];
		}

		this.urlBase = this.getProtocol() + this.getHost() + getUri();
		this.urlBase = this.urlBase.substring(0, this.urlBase.lastIndexOf("/") + 1);
		this.hostBase = this.protocol + this.host + "/";

	}

	private String protocol;
	private String url;
	private String host;
	private String uriSearch;
	private String uri;
	private String search;
	private String urlBase;
	private String hostBase;

	public String toString() {
		return toURL();
	}

	public String toURL() {
		return this.protocol + this.host + this.uri + (this.search == null ? "" : "?" + this.search);
	}

	public String getHostBase() {
		return hostBase;
	}

	public void setHostBase(String hostBase) {
		this.hostBase = hostBase;
	}

	public String getUrlBase() {
		return urlBase;
	}

	public void setUrlBase(String urlBase) {
		this.urlBase = urlBase;
	}

	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getUriSearch() {
		return uriSearch;
	}

	public void setUriSearch(String uriSearch) {
		this.uriSearch = uriSearch;
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

}
